<template>
  <view class="profile-container">
    <!-- 抖音一键授权提示（底部上滑），进入页面直接展示，可拒绝/同意 -->
    <SilentLogin ref="silentLogin" :autoLogin="false" :preferPrompt="false" :delayShow="0" />
    <!-- 顶部用户信息卡片 -->
    <view class="user-card">
      <view class="avatar-section">
        <image class="avatar" src="/static/logo.png" mode="aspectFill" />
        <view class="user-info">
          <text class="username" @click="onClickUsername">用户名</text>
          <text class="user-desc">探索艺术的美好世界</text>
        </view>
      </view>
    </view>

    <!-- 功能菜单 -->
    <view class="menu-section">
      <view class="menu-item" @click="openAddress">
        <view class="menu-left">
          <uni-icons type="home-filled" size="24" color="#0FFDCD" />
          <text class="menu-text">地址管理（占位）</text>
        </view>
        <uni-icons type="arrow-right" size="16" color="#cccccc" />
      </view>

      <view class="menu-item" @click="joinGroup">
        <view class="menu-left">
          <uni-icons type="chat" size="24" color="#0FFDCD" />
          <text class="menu-text">加入用户群</text>
        </view>
        <uni-icons type="arrow-right" size="16" color="#cccccc" />
      </view>
    </view>

    <!-- 设置菜单 -->
    <view class="menu-section">
      <view class="menu-item" @click="loginNow" v-if="!isLoggedIn">
        <view class="menu-left">
          <uni-icons type="person-add" size="24" color="#0FFDCD" />
          <text class="menu-text">登录</text>
        </view>
        <uni-icons type="arrow-right" size="16" color="#cccccc" />
      </view>

      <view class="menu-item" @click="logoutNow" v-else>
        <view class="menu-left">
          <uni-icons type="closeempty" size="24" color="#0FFDCD" />
          <text class="menu-text">退出登录</text>
        </view>
        <uni-icons type="arrow-right" size="16" color="#cccccc" />
      </view>

      <view class="menu-item" @click="openSettings">
        <view class="menu-left">
          <uni-icons type="settings-filled" size="24" color="#0FFDCD" />
          <text class="menu-text">设置</text>
        </view>
        <uni-icons type="arrow-right" size="16" color="#cccccc" />
      </view>
      
      <view class="menu-item">
        <view class="menu-left">
          <uni-icons type="help-filled" size="24" color="#0FFDCD" />
          <text class="menu-text">帮助与反馈</text>
        </view>
        <uni-icons type="arrow-right" size="16" color="#cccccc" />
      </view>
      
      <view class="menu-item">
        <view class="menu-left">
          <uni-icons type="info-filled" size="24" color="#0FFDCD" />
          <text class="menu-text">关于我们</text>
        </view>
        <uni-icons type="arrow-right" size="16" color="#cccccc" />
      </view>
    </view>

    <!-- 自定义底部导航栏 -->
    <CustomTabBar :currentIndex="3" />
  </view>
</template>

<script>
import CustomTabBar from '@/components/CustomTabBar.vue'
import SilentLogin from '@/components/SilentLogin.vue'
import { loginController } from '@/utils/login.js'

export default {
  name: 'Profile',
  components: {
    CustomTabBar,
    SilentLogin
  },
  data() {
    return {
      isLoggedIn: false,
      userId: null
    }
  },
  created() {
    this.refreshLoginStatus()
  },
  methods: {
    navigateToPage(url) {
      uni.navigateTo({ url })
    },
    onClickUsername() {
      // 点击用户名，拉起授权提示或提示模拟登录
      if (typeof tt !== 'undefined') {
        if (this.$refs.silentLogin && this.$refs.silentLogin.openPrompt) {
          this.$refs.silentLogin.openPrompt()
        }
      } else {
        uni.showToast({ title: '当前为模拟登录', icon: 'none' })
      }
    },
    refreshLoginStatus() {
      try {
        this.isLoggedIn = loginController.checkLoginStatus()
        this.userId = this.isLoggedIn ? loginController.getUserId() : null
      } catch (e) { this.isLoggedIn = false; this.userId = null }
    },
    loginNow() {
      if (this.$refs.silentLogin && this.$refs.silentLogin.openPrompt) {
        this.$refs.silentLogin.openPrompt()
      } else {
        uni.showToast({ title: '无法打开登录', icon: 'none' })
      }
    },
    async logoutNow() {
      try {
        await loginController.logout()
        this.isLoggedIn = false
        this.userId = null
        uni.showToast({ title: '已退出登录', icon: 'success' })
      } catch (e) {
        uni.showToast({ title: '退出失败', icon: 'none' })
      }
    },
    openAddress() {
      // TODO: 跳转到“地址管理”页面，当前先占位
      uni.showToast({ title: '地址管理（占位）', icon: 'none' })
    },
    joinGroup() {
      // TODO: 替换为真实群邀请链接或二维码页
      uni.showModal({
        title: '加入用户群',
        content: '请添加客服或扫描二维码加入用户群（占位）',
        showCancel: false
      })
    },
    openSettings() {
      // TODO: 跳转设置页，实现通知、隐私等开关
      uni.showToast({ title: '设置（占位）', icon: 'none' })
    }
  }
}
</script>

<style lang="scss" scoped>
.profile-container {
  min-height: 100vh;
  background: #1a1a1a;
  padding: 20px;
  padding-bottom: 120px; /* 为底部导航栏留出空间 */
}

.user-card {
  background: #333333;
  border-radius: 20px;
  padding: 24px;
  margin-bottom: 24px;
  border: 1px solid #444444;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
  display: flex;
  align-items: center;
  justify-content: space-between;
  
  .avatar-section {
    display: flex;
    align-items: center;
    
    .avatar {
      width: 60px;
      height: 60px;
      border-radius: 30px;
      border: 3px solid rgba(212, 175, 55, 0.5);
      margin-right: 16px;
    }
    
    .user-info {
      display: flex;
      flex-direction: column;
      
      .username {
        font-size: 20px;
        font-weight: 600;
        color: #ffffff;
        font-family: 'Fugaz One', 'PingFang SC', 'Microsoft YaHei', sans-serif;
        margin-bottom: 4px;
      }
      
      .user-desc {
        font-size: 14px;
        color: #cccccc;
        font-family: 'Fugaz One', 'PingFang SC', 'Microsoft YaHei', sans-serif;
      }
    }
  }
}

.menu-section {
  background: #333333;
  border-radius: 16px;
  margin-bottom: 16px;
  border: 1px solid #444444;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.3);
  overflow: hidden;
  
  .menu-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid #444444;
    transition: all 0.3s ease;
    
    &:last-child {
      border-bottom: none;
    }
    
    &:active {
      background: rgba(255, 255, 255, 0.05);
      transform: scale(0.98);
    }
    
    .menu-left {
      display: flex;
      align-items: center;
      
      .menu-text {
        font-size: 16px;
        color: #ffffff;
        font-family: 'Fugaz One', 'PingFang SC', 'Microsoft YaHei', sans-serif;
        font-weight: 500;
        margin-left: 12px;
      }
    }
  }
}

/* 页面进入动画 */
.profile-container {
  animation: fadeInUp 0.6s ease-out;
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 适配不同屏幕尺寸 */
@media screen and (max-width: 375px) {
  .profile-container {
    padding: 16px;
  }
  
  .user-card {
    padding: 20px;
    
    .avatar-section .avatar {
      width: 50px;
      height: 50px;
      border-radius: 25px;
    }
    
    .user-info .username {
      font-size: 18px;
    }
  }
  
  .menu-section .menu-item {
    padding: 14px 16px;
    
    .menu-left .menu-text {
      font-size: 15px;
    }
  }
}
</style>